<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div {
        width: 100px;
        height: 100px;
        box-sizing: border-box;
      }
      .box1 {
        background-color: red;
      }
      .box2 {
        background-color: blue;
      }
      .box3 {
        background-color: yellow;
      }
      .active {
        border: 5px solid;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="box1"></div>
      <div class="box2"></div>
      <div class="box3"></div>
    </div>
    <script>
      //通过循环来绑定时间,更加灵活
      var divs = document.querySelectorAll(".container div");
      //console.log(divs)
      //循环每一个元素绑定点击事件
      for (var i = 0; i < divs.length; i++) {
        divs[i].onclick = function () {
          for (j = 0; j < divs.length; j++) {
            divs[j].classList.remove("active");
          }
          this.classList.add("active");
        };
      }
    </script>
  </body>
</html>
